﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Maps.Controls;
using Windows.Devices.Geolocation;
using System.Device.Location;
using PhoneMap.Controls;
using PhoneMap.ViewModel;
using System.Windows.Media;

namespace PhoneMap.View
{
    public partial class ViewPostion : PhoneApplicationPage
    {
        MapLayer PushpinMapLayer;
        private List<GeoCoordinate> listPostion;
        ProgressIndicator pi;
        public ViewPostion()
        {
            InitializeComponent();
            listPostion = new List<GeoCoordinate>();
            Loaded += ViewPostion_Loaded;
            pi = new ProgressIndicator();
            pi.IsIndeterminate = true;
            pi.IsVisible = false;
        }

        async void ViewPostion_Loaded(object sender, RoutedEventArgs e)
        {
            await ShowPostion();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //Creating a MapLayer and adding the MapOverlay to it
            PushpinMapLayer = new MapLayer();
            MyMap.Layers.Add(PushpinMapLayer);

        }

        private async System.Threading.Tasks.Task ShowPostion()
        {
            pi.IsVisible = true;
            pi.IsIndeterminate = true;
            pi.Text = "Resolving...";
            SystemTray.SetProgressIndicator(this, pi);
            this.Opacity = 0.5;
            //Getting Phone's current location
            Geolocator MyGeolocator = new Geolocator();
            MyGeolocator.DesiredAccuracyInMeters = 5;
            Geoposition myGeoPosition = null;
            try
            {
                myGeoPosition = await MyGeolocator.GetGeopositionAsync(TimeSpan.FromMinutes(1), TimeSpan.FromSeconds(10));
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Location is disabled in phone settings");
            }

            var getLocationMy = new GeoCoordinate(myGeoPosition.Coordinate.Latitude, myGeoPosition.Coordinate.Longitude);

            //Creating a MapOverlay and adding the Pushpin to it.
            MapOverlay MyOverlay = new MapOverlay();
            var showPost = new PushPinControl(string.Empty,string.Empty);
            showPost.Avatar.Fill = new SolidColorBrush(Colors.Red);
            MyOverlay.Content = showPost;
            MyOverlay.GeoCoordinate = getLocationMy;
            MyOverlay.PositionOrigin = new Point(0, 0.5);

            // Add the MapOverlay containing the pushpin to the MapLayer
            this.PushpinMapLayer.Add(MyOverlay);
            // Set the center on the map
            foreach (var data in LoginViewModel.ListPostions)
            {
                var getLocation = new GeoCoordinate(data.latitude, data.longitude);
                //Creating a MapOverlay and adding the Pushpin to it.
                MapOverlay myPost = new MapOverlay();
                if(data.user_id!=null)
                    myPost.Content = new PushPinControl(string.Empty,data.user_id);
                myPost.GeoCoordinate = getLocation;
                myPost.PositionOrigin = new Point(0, 0.5);
                // Add the MapOverlay containing the pushpin to the MapLayer
                this.PushpinMapLayer.Add(myPost);
            }

            pi.IsVisible = false;
            pi.IsIndeterminate = false;
            pi.Text = "Complete";
            SystemTray.SetProgressIndicator(this, pi);
            this.Opacity = 1;

           
            MyMap.Center = getLocationMy;
            MyMap.ZoomLevel = 10;


        }
        private void mnuRoad_Click(object sender, EventArgs e)
        {
            MyMap.CartographicMode = MapCartographicMode.Road;
        }

        private void mnuAerial_Click(object sender, EventArgs e)
        {
            MyMap.CartographicMode = MapCartographicMode.Aerial;
        }

        private void mnuHybrid_Click(object sender, EventArgs e)
        {
            MyMap.CartographicMode = MapCartographicMode.Hybrid;
        }

        private void mnuTerrain_Click(object sender, EventArgs e)
        {
            MyMap.CartographicMode = MapCartographicMode.Terrain;
        }

        private void mnuLight_Click(object sender, EventArgs e)
        {
            MyMap.ColorMode = MapColorMode.Light;
        }

        private void mnuDark_Click(object sender, EventArgs e)
        {
            MyMap.ColorMode = MapColorMode.Dark;
        }
    }
}